<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>报告列表 - AI 职业评估系统</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    
    <!-- Tailwind 配置 -->
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: '#4F46E5',
                        secondary: '#10B981',
                        neutral: '#1F2937',
                        chatBg: '#F9FAFB',
                    },
                    fontFamily: {
                        inter: ['Inter', 'system-ui', 'sans-serif'],
                    },
                },
            }
        }
    </script>
    
    <style type="text/tailwindcss">
        @layer utilities {
            .content-auto {
                content-visibility: auto;
            }
            .scrollbar-hide::-webkit-scrollbar {
                display: none;
            }
            .scrollbar-hide {
                -ms-overflow-style: none;
                scrollbar-width: none;
            }
            .report-card-hover {
                transition: all 0.3s ease;
            }
            .report-card-hover:hover {
                transform: translateY(-2px);
                box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
            }
        }
    </style>
</head>
<body class="font-inter bg-gray-50 min-h-screen">
    <div class="flex flex-col min-h-screen">
        <!-- 顶部导航栏 -->
        <header class="bg-white shadow-md">
            <div class="container mx-auto px-4 py-4 flex justify-between items-center">
                <div class="flex items-center space-x-2">
                    <i class="fa fa-robot text-primary text-2xl"></i>
                    <h1 class="text-xl font-bold text-neutral">AI 职业评估系统</h1>
                </div>
                <div class="flex items-center space-x-4">
                    <a href="/chatbot.html" class="flex items-center space-x-1 text-sm text-gray-600 hover:text-primary transition-colors duration-200">
                        <i class="fa fa-comments"></i>
                        <span>返回聊天</span>
                    </a>
                    <div class="w-8 h-8 rounded-full bg-primary/10 flex items-center justify-center text-primary">
                        <i class="fa fa-user"></i>
                    </div>
                </div>
            </div>
        </header>
        
        <!-- 主要内容区域 -->
        <main class="flex-1 container mx-auto px-4 py-6 max-w-5xl">
            <div class="bg-white rounded-2xl shadow-md p-6 mb-6">
                <div class="flex justify-between items-center mb-6">
                    <h2 class="text-2xl font-bold text-neutral flex items-center">
                        <i class="fa fa-file-text-o text-primary mr-3"></i>
                        评估报告列表
                    </h2>
                    <button id="refresh-btn" class="flex items-center space-x-1 text-sm bg-primary hover:bg-primary/90 text-white px-4 py-2 rounded-lg transition-colors duration-200">
                        <i class="fa fa-refresh"></i>
                        <span>刷新列表</span>
                    </button>
                </div>
                
                <!-- 加载状态 -->
                <div id="loading" class="flex justify-center items-center py-12">
                    <div class="flex flex-col items-center">
                        <div class="w-12 h-12 border-4 border-primary border-t-transparent rounded-full animate-spin mb-4"></div>
                        <p class="text-gray-500">正在加载报告...</p>
                    </div>
                </div>
                
                <!-- 空状态 -->
                <div id="empty-state" class="hidden flex flex-col items-center justify-center py-16 text-center">
                    <div class="w-24 h-24 rounded-full bg-gray-100 flex items-center justify-center text-gray-400 mb-6">
                        <i class="fa fa-file-text-o text-4xl"></i>
                    </div>
                    <h3 class="text-xl font-medium text-gray-800 mb-2">暂无评估报告</h3>
                    <p class="text-gray-500 max-w-md">完成职业评估对话后，您的报告会显示在这里。</p>
                    <a href="/chatbot.html" class="mt-6 bg-primary hover:bg-primary/90 text-white px-6 py-2 rounded-lg transition-colors duration-200 inline-flex items-center">
                        <i class="fa fa-comments-o mr-2"></i>
                        开始评估
                    </a>
                </div>
                
                <!-- 报告列表 -->
                <div id="reports-container" class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <!-- 报告卡片将通过JavaScript动态插入 -->
                </div>
            </div>
        </main>
        
        <!-- 底部信息 -->
        <footer class="bg-white py-4 border-t border-gray-100">
            <div class="container mx-auto px-4 text-center text-sm text-gray-500">
                <p>© 2024 AI 职业评估系统 | 基于 Spring Boot 开发</p>
            </div>
        </footer>
    </div>
    
    <!-- 报告详情模态框 -->
    <div id="report-modal" class="fixed inset-0 bg-black/50 flex items-center justify-center z-50 hidden">
        <div class="bg-white rounded-2xl shadow-xl w-full max-w-4xl max-h-[90vh] overflow-hidden flex flex-col">
            <div class="p-6 border-b border-gray-200 flex justify-between items-center">
                <h3 class="text-xl font-bold text-neutral" id="modal-title">报告详情</h3>
                <button id="close-modal" class="text-gray-500 hover:text-gray-700 transition-colors">
                    <i class="fa fa-times text-xl"></i>
                </button>
            </div>
            <div class="p-6 overflow-y-auto flex-1" id="modal-content">
                <!-- 报告内容将通过JavaScript动态插入 -->
            </div>
        </div>
    </div>
    
    <script>
        // DOM 元素
        const reportsContainer = document.getElementById('reports-container');
        const loadingElement = document.getElementById('loading');
        const emptyStateElement = document.getElementById('empty-state');
        const refreshBtn = document.getElementById('refresh-btn');
        const reportModal = document.getElementById('report-modal');
        const modalTitle = document.getElementById('modal-title');
        const modalContent = document.getElementById('modal-content');
        const closeModal = document.getElementById('close-modal');
        
        // 页面加载时获取报告列表
        document.addEventListener('DOMContentLoaded', () => {
            fetchReports();
        });
        
        // 刷新按钮事件监听
        refreshBtn.addEventListener('click', fetchReports);
        
        // 关闭模态框事件监听
        closeModal.addEventListener('click', () => {
            reportModal.classList.add('hidden');
        });
        
        // 点击模态框外部关闭
        reportModal.addEventListener('click', (e) => {
            if (e.target === reportModal) {
                reportModal.classList.add('hidden');
            }
        });
        
        // 获取报告列表
        async function fetchReports() {
            // 显示加载状态
            loadingElement.classList.remove('hidden');
            reportsContainer.innerHTML = '';
            emptyStateElement.classList.add('hidden');
            
            try {
                const response = await fetch('http://localhost:8080/ai/listReport');
                
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                
                const reports = await response.json();
                
                // 隐藏加载状态
                loadingElement.classList.add('hidden');
                
                // 检查是否有报告
                if (!reports || reports.length === 0) {
                    emptyStateElement.classList.remove('hidden');
                    return;
                }
                
                // 按创建时间倒序排列
                reports.sort((a, b) => new Date(b.createTime) - new Date(a.createTime));
                
                // 渲染报告列表
                renderReports(reports);
                
            } catch (error) {
                console.error('Error fetching reports:', error);
                loadingElement.classList.add('hidden');
                
                // 显示错误状态
                reportsContainer.innerHTML = `
                    <div class="col-span-full text-center py-12">
                        <div class="w-16 h-16 rounded-full bg-red-100 flex items-center justify-center text-red-500 mx-auto mb-4">
                            <i class="fa fa-exclamation-triangle text-2xl"></i>
                        </div>
                        <h3 class="text-lg font-medium text-gray-800 mb-2">加载失败</h3>
                        <p class="text-gray-500 mb-4">无法加载报告列表，请稍后再试。</p>
                        <button onclick="fetchReports()" class="bg-primary hover:bg-primary/90 text-white px-4 py-2 rounded-lg transition-colors duration-200">
                            重试
                        </button>
                    </div>
                `;
            }
        }
        
        // 渲染报告列表
        function renderReports(reports) {
            reports.forEach(report => {
                // 解析报告详情中的JSON
                let summary = '暂无摘要信息';
                let totalScore = '';
                let rating = '';
                
                try {
                    const detail = JSON.parse(report.detail);
                    if (detail.overallAssessment) {
                        summary = detail.overallAssessment.summary || summary;
                        totalScore = detail.overallAssessment.totalScore ? `总分: ${detail.overallAssessment.totalScore}` : '';
                        rating = detail.overallAssessment.rating ? detail.overallAssessment.rating : '';
                    }
                } catch (e) {
                    console.error('Error parsing report detail:', e);
                }
                
                // 格式化日期
                const formattedDate = new Date(report.createTime).toLocaleString('zh-CN', {
                    year: 'numeric',
                    month: '2-digit',
                    day: '2-digit',
                    hour: '2-digit',
                    minute: '2-digit'
                });
                
                // 创建报告卡片
                const reportCard = document.createElement('div');
                reportCard.className = 'bg-white rounded-xl border border-gray-200 p-4 report-card-hover cursor-pointer';
                reportCard.innerHTML = `
                    <div class="flex justify-between items-start mb-3">
                        <h3 class="font-semibold text-lg text-gray-800 truncate">${getReportTitle(report.id)}</h3>
                        <span class="px-2 py-1 text-xs font-medium rounded-full ${getRatingClass(rating)}">${rating}</span>
                    </div>
                    <p class="text-gray-600 text-sm line-clamp-2 mb-3 h-12">${summary}</p>
                    <div class="flex justify-between items-center text-xs text-gray-500">
                        <span>${formattedDate}</span>
                        <span>${totalScore}</span>
                    </div>
                `;
                
                // 添加点击事件显示详情
                reportCard.addEventListener('click', () => {
                    showReportDetail(report);
                });
                
                reportsContainer.appendChild(reportCard);
            });
        }
        
        // 显示报告详情
        function showReportDetail(report) {
            modalTitle.textContent = `${getReportTitle(report.id)} - 详细报告`;
            
            try {
                const detail = JSON.parse(report.detail);
                modalContent.innerHTML = generateReportHtml(detail);
            } catch (e) {
                console.error('Error parsing report detail:', e);
                modalContent.innerHTML = `
                    <div class="text-center py-8">
                        <p class="text-red-600">报告内容解析失败</p>
                        <pre class="mt-4 p-4 bg-gray-100 rounded-lg text-sm overflow-auto">${escapeHtml(report.detail)}</pre>
                    </div>
                `;
            }
            
            reportModal.classList.remove('hidden');
        }
        
        // 生成报告HTML
        function generateReportHtml(detail) {
            let html = '';
            
            // 总体评估
            if (detail.overallAssessment) {
                html += `
                <div class="mb-6">
                    <h4 class="text-lg font-semibold text-neutral mb-3 flex items-center">
                        <i class="fa fa-trophy text-primary mr-2"></i>
                        总体评估
                    </h4>
                    <div class="bg-gray-50 p-4 rounded-lg border border-gray-100">
                        <div class="flex flex-wrap items-center gap-4 mb-3">
                            <div class="flex items-center">
                                <span class="text-gray-600 mr-2">评级：</span>
                                <span class="font-semibold ${getRatingClass(detail.overallAssessment.rating)}">${detail.overallAssessment.rating}</span>
                            </div>
                            <div class="flex items-center">
                                <span class="text-gray-600 mr-2">总分：</span>
                                <span class="font-semibold text-primary">${detail.overallAssessment.totalScore}</span>
                            </div>
                        </div>
                        <p class="text-gray-700">${detail.overallAssessment.summary}</p>
                    </div>
                </div>
                `;
            }
            
            // 维度详情
            if (detail.dimensionDetails) {
                html += `
                <div class="mb-6">
                    <h4 class="text-lg font-semibold text-neutral mb-3 flex items-center">
                        <i class="fa fa-list text-primary mr-2"></i>
                        维度详情
                    </h4>
                `;
                
                Object.values(detail.dimensionDetails).forEach(dimension => {
                    html += `
                    <div class="mb-4 p-4 rounded-lg border border-gray-200">
                        <div class="flex justify-between items-center mb-2">
                            <h5 class="font-medium text-gray-800">${dimension.dimension}</h5>
                            <div class="flex items-center gap-2">
                                <span class="font-semibold text-primary">${dimension.score}</span>
                                <span class="px-2 py-0.5 text-xs font-medium rounded-full ${getRatingClass(dimension.level)}">${dimension.level}</span>
                            </div>
                        </div>
                        <p class="text-gray-600 text-sm mb-3">${dimension.detailedAnalysis}</p>
                        ${generateSuggestionsHtml(dimension.suggestions)}
                    </div>
                    `;
                });
                
                html += `</div>`;
            }
            
            return html || '<p class="text-center text-gray-500 py-8">报告内容不完整</p>';
        }
        
        // 生成建议HTML
        function generateSuggestionsHtml(suggestions) {
            if (!suggestions || suggestions.length === 0) return '';
            
            let html = `
            <div class="mt-3">
                <h6 class="text-sm font-medium text-gray-700 mb-2">改进建议：</h6>
                <ul class="list-disc list-inside text-gray-600 text-sm space-y-1">
            `;
            
            suggestions.forEach(suggestion => {
                html += `<li>${suggestion}</li>`;
            });
            
            html += `</ul></div>`;
            return html;
        }
        
        // 获取报告标题
        function getReportTitle(reportId) {
            // 从报告ID中提取日期信息作为标题
            const dateMatch = reportId.match(/report_(\d{8})/);
            if (dateMatch && dateMatch[1]) {
                const dateStr = dateMatch[1];
                const year = dateStr.substring(0, 4);
                const month = dateStr.substring(4, 6);
                const day = dateStr.substring(6, 8);
                return `${year}年${month}月${day}日 评估报告`;
            }
            return '职业评估报告';
        }
        
        // 根据评级获取样式类
        function getRatingClass(rating) {
            switch (rating) {
                case '优秀':
                    return 'bg-green-100 text-green-800';
                case '良好':
                    return 'bg-blue-100 text-blue-800';
                case '一般':
                    return 'bg-yellow-100 text-yellow-800';
                case '较差':
                    return 'bg-red-100 text-red-800';
                default:
                    return 'bg-gray-100 text-gray-800';
            }
        }
        
        // HTML转义
        function escapeHtml(text) {
            const div = document.createElement('div');
            div.textContent = text;
            return div.innerHTML;
        }
    </script>
</body>
</html>